package com.jp.knowledge.activity;


import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.fragment.AttentionDynamicFragment;
import com.jp.knowledge.fragment.AttentionListOfNameFragment;
import com.jp.knowledge.fragment.AttentionStatisticsFragment;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/3/18.
 */

public class AttentionGroupDetailInfo extends SlidingActivity implements View.OnClickListener{

    @ViewInject(R.id.attention_tablayout)
    protected TabLayout tabLayout;
    @ViewInject(R.id.attention_viewpager)
    protected ViewPager viewPager;

    protected String id;
    protected String title;
    protected int type;
    protected String navTypeId;
    protected List<Fragment> pageFragments;

    @Override
    protected int getResourceId() {
        return R.layout.activity_attention_group_detail_info;
    }

    @Override
    protected void init() {
        this.id = getIntent().getStringExtra("id");
        this.title = getIntent().getStringExtra("title");
        this.type = getIntent().getIntExtra("type", 9);
        this.navTypeId = getIntent().getStringExtra("navTypeId");
        initTitleBar();
        pageFragments = new ArrayList<>();
        pageFragments.add(AttentionDynamicFragment.newInstance(id, type, navTypeId));
        pageFragments.add(AttentionStatisticsFragment.newInstance(id, type));
        pageFragments.add(AttentionListOfNameFragment.newInstance(id, type));
        viewPager.setOffscreenPageLimit(pageFragments.size());
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return pageFragments.size();
            }
            @Override
            public Fragment getItem(int position) {
                return pageFragments.get(position);
            }
        });
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
            @Override
            public void onPageScrollStateChanged(int state) {}
            @Override
            public void onPageSelected(int position) {
                if(position == 2)
                    rightIcon.setVisibility(View.VISIBLE);
                else
                    rightIcon.setVisibility(View.GONE);
            }
        });
        tabLayout.setupWithViewPager(viewPager, true);
        tabLayout.getTabAt(0).setText("动态");
        tabLayout.getTabAt(1).setText("统计");
        tabLayout.getTabAt(2).setText("名单");
    }

    protected void initTitleBar(){
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        leftIcon.setOnClickListener(this);
        rightIcon.setVisibility(View.GONE);
        rightIcon.setImageResource(R.mipmap.guanzhutianjia);
        rightIcon.setOnClickListener(this);
        topName.setText(title);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.icon_left) {
            finish();
        } else if(v.getId() == R.id.icon_serach){
            startActivity(new Intent(mContext, SearchAttentionActivity.class)
                    .putExtra("groupId", id));
        }
    }
}
